﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Red.Core;

namespace Red.Controls.ViewModels
{
    public class BreadcrumbBarItem : ViewModel
    {
        #region Data

        private readonly BreadcrumbBar _parent;
        private readonly BreadcrumbBarItem _prev;
        private RedIcon _icon;

        #endregion Data

        #region Properties

        /// <summary>
        ///     Displayed item text
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        ///     How the element appears in the path
        /// </summary>
        public string PathElement { get; private set; }

        /// <summary>
        ///     Original raw description of the element as passed to the constructor
        /// </summary>
        internal string Description { get; private set; }

        /// <summary>
        ///    Full item path 
        /// </summary>
        public string Path
        {
            get
            {
                var path = new StringBuilder();
                for ( BreadcrumbBarItem item = this; item != null; item = item._prev )
                {
                    if ( item != this )
                        path.Insert( 0, _parent.Separator );
                    path.Insert( 0, item.PathElement );
                }
                return path.ToString();
            }
        }

        /// <summary>
        ///     
        /// </summary>
        public object Content
        {
            get
            {
                if ( _icon != null )
                    return _icon;
                else
                    return Title;
            }
        }

        /// <summary>
        /// List of elements for the drop-down
        /// </summary>
        public BindableAsyncValue<List<string>> Children
        {
            get
            {
                return new BindableAsyncValue<List<string>>( _parent.GetBranchesForItem( this ) );
            }
        }

        #endregion Properties

        #region C-tor

        public BreadcrumbBarItem( BreadcrumbBar parent, BreadcrumbBarItem prev, string description )
        {
            _parent = parent;
            _prev = prev;
            Description = description;
            DecodeDescription( description );
        }

        #endregion C-tor

        #region Private methods
        
        private void DecodeDescription( string description )
        {
            if ( description.StartsWith( "{" ) && description.EndsWith( "}" ) )
            {
                string[] parts = description.Substring( 1, description.Length-2 ).Split( new[] { ',' } );
                if ( parts.Length == 1 ) // {title}
                {
                    Title = parts[ 0 ];
                    PathElement = Title;
                }
                else if ( parts.Length == 2 ) // {title,path} or {title,icon}
                {
                    Title = parts[ 0 ];
                    if ( ExtractImage( parts[ 1 ] ) )
                        PathElement = Title;
                    else
                        PathElement = parts[ 1 ];
                }
                else if ( parts.Length == 3 ) // {title,path,icon}
                {
                    Title = parts[ 0 ];
                    PathElement = parts[ 1 ];
                    ExtractImage( parts[ 2 ] );
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            else
            {
                Title = description;
                PathElement = Title;
            }
        }

        private bool ExtractImage( string description )
        {
            if ( description.StartsWith( "icon:" ) )
            {
                var imgName = description.Substring( 5 );
                var canvas = Application.Current.TryFindResource( imgName ) as Canvas;
                if ( canvas != null )
                {
                    _icon = new RedIcon() { Width = 16, Height = 16, Canvas = canvas };
                    return true;
                }
            }
            return false;
        }
        
        #endregion Private methods
    }

} // namespace Red.Controls
